/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.panels.importDSpanel;

import java.awt.event.ActionEvent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import logic.ControllerAL_Main;
import model.dataset.Dataset;
import view.controllers.ControllerUI_DataSet;
import view.controllers.ControllerUI_Main;
import view.panels.datasets.FrmDataSet;
import view.panels.importDSpanel.model.TableModelDataSets;

/**
 *
 * @author Jelena
 */
public class PanelAllDataSets extends javax.swing.JPanel {

    /**
     * Creates new form PanelAllDataSets
     */
    public PanelAllDataSets() {
        initComponents();
        TableModelDataSets tmds = new TableModelDataSets(ControllerAL_Main.getInstance().getAllDataSets());
        tblDatasets.setModel(tmds);
        tblDatasets.setComponentPopupMenu(postaviPopupmenu());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblDatasets = new javax.swing.JTable();

        jScrollPane1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        tblDatasets.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tblDatasets.setToolTipText("");
        tblDatasets.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblDatasetsMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblDatasets);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 719, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tblDatasetsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblDatasetsMouseClicked
//        if (SwingUtilities.isRightMouseButton(evt)) {
//            JPopupMenu popupMeny = new JPopupMenu();
//            JMenuItem miViewDataset = new JMenuItem("View dataset");
//            tblDatasets.getSelectedRow();
//
//            miViewDataset.addActionListener(new java.awt.event.ActionListener() {
//                @Override
//                public void actionPerformed(java.awt.event.ActionEvent evt) {
//                    FrmDataSet dsForm = new FrmDataSet();
//                    dsForm.setVisible(true);
//                    dsForm.setFocusable(true);
//                }
//            });
//
//            popupMeny.add(miViewDataset);
//            popupMeny.show(evt.getComponent(), evt.getX(), evt.getY());
//            popupMeny.setVisible(true);
//            popupMeny.setFocusable(true);
//        }
    }//GEN-LAST:event_tblDatasetsMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblDatasets;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the tblDatasets
     */
    public javax.swing.JTable getTblDatasets() {
        return tblDatasets;
    }
    
        private JPopupMenu postaviPopupmenu() {
        JPopupMenu jpm = new JPopupMenu("Dataset");
        JMenuItem jmiDetalji = new JMenuItem();
        jmiDetalji.setText("View details about dataset");
        jmiDetalji.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiDetaljiActionPerformed(evt);
            }

            private void jmiDetaljiActionPerformed(ActionEvent evt) {
                int red = tblDatasets.getSelectedRow();
                Dataset ds =((TableModelDataSets)tblDatasets.getModel()).getDataSet(red);
                FrmDataSet fds= new FrmDataSet(ControllerUI_Main.getInstance().getMainForm(), true);
                ControllerUI_DataSet cads=new ControllerUI_DataSet(fds);
                fds.setControllerDataSet(cads);
                cads.setOdo(ds);
                cads.setOef(fds);
                cads.KonvertujDomenskiObjekatUGrafickiObjekat();
                fds.getBtnUpdate().setVisible(false);
                fds.setVisible(true);
            }
        });
        jpm.add(jmiDetalji);
        
        
        JMenuItem jmiIzmeni = new JMenuItem();
        jmiIzmeni.setText("Edit dataset");
        jmiIzmeni.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiIzmeniActionPerformed(evt);
            }

            private void jmiIzmeniActionPerformed(ActionEvent evt) {
                int red = tblDatasets.getSelectedRow();
                Dataset ds =((TableModelDataSets)tblDatasets.getModel()).getDataSet(red);
                FrmDataSet fds= new FrmDataSet(ControllerUI_Main.getInstance().getMainForm(), true);
                ControllerUI_DataSet cads=new ControllerUI_DataSet(fds);
                fds.setControllerDataSet(cads);
                cads.setOdo(ds);
                cads.setOef(fds);
                cads.KonvertujDomenskiObjekatUGrafickiObjekat();
                fds.getBtnUpdate().setVisible(true);
                fds.setVisible(true);
            }
        });
        jpm.add(jmiIzmeni);
        
        return jpm;
}
}
